package me.raih.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Raih
 * @Time 2020/11/23  9:14
 *
 * Druid数据配置
 */
@Configuration
public class DruidConfig {

    //使用 配置里的 属性
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){

        return new DruidDataSource();
    }

    //后台监控
    @Bean
    public ServletRegistrationBean statViewServlet(){

        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        Map<String,String> initMap = new HashMap<>();
        //设置用户名密码
        initMap.put("loginUsername","admin");
        initMap.put("loginPassword","123456");
        //白名单
        initMap.put("allow","");
        //黑名单
        //initMap.put("deny","192.168.100.100");

        bean.setInitParameters(initMap);
        return bean;
    }

}
